var loginHostUrl = "http://my.youboy.com/"
$(document).ready(function() {

	// $("#captcha-wrap").hide();

	$("#password").keydown(function(e) {
		if (e.keyCode == 13 && kw != "") {
			login();
		}
	});

	$("#username").keydown(function(e) {
		if (e.keyCode == 13 && kw != "") {
			login();
		}
	});

});

function showAlertBox(title, content){
	$(".layer").show();
	$(".alert-box .title h3").text(title);
	$(".alert-box .content p").text(content);
	$(".alert-box").removeClass("slide").addClass("shake").show();
}

function login() {
	var username = $("#username").val().toLocaleLowerCase();
	var password = $("#password").val();
	
	if(username == null || $.trim(username) == ""){
		showAlertBox("绑定失败", "请输入要绑定的商铺用户名！");
		return;
	}
	
	var reId = new RegExp("^[a-zA-Z0-9_]{3,20}$", "g");
	var b_id = reId.test(username);
	if(!b_id){
		showAlertBox("绑定失败", "用户名错误，请重新输入！");
		return;
	}
	
	if(password == null || $.trim(password) == ""){
		showAlertBox("绑定失败", "请输入密码！");
		return;
	}
	
	if(password.length < 6){
		showAlertBox("绑定失败", "密码错误！");
		return;
	}
	
	var captcha = $("#code").val();
	password = encodeURIComponent(password);
	var data = "user=" + username + "&pass=" + password + "&check=" + captcha;
	
	$.getJSON(loginHostUrl + "myAjaxLogin.form?" + data + "&jsoncallback=?",
			function(json) {
				var result = json.result;
				if (result == 'LOGIN_SUCCESS') {
					$("#bindForm").submit();
				} else if (result == "PARAMETER_FORMAT_ERROR") {
					showAlertBox("绑定失败", "用户名或密码格式错误");
				} else if (result == "USER_LIMITED") {
					showAlertBox("绑定失败", "对不起！该用户被限制，请联系客服");
				} else if (result == "USER_LIMITED_AND_CHECK") {
					showAlertBox("绑定失败", "对不起！该用户被限制，请联系客服");
				} else if (result == "USERNAME_OR_PASSWORD_ERROR") {
					showAlertBox("绑定失败", "用户名或密码错误！");
				} else if (result == "LOGIN_FREQUENT") {
					showAlertBox("绑定失败", "您绑定过于频繁请30分钟后再试！");
				} else if (result == "USERNAME_OR_PASSWORD_ERROR_AND_CHECK") {
					changeCaptcha();
					showAlertBox("绑定失败", "用户名或密码错误,请输入验证码！");
				} else if (result == "CHECKCODE_ERROR") {
					changeCaptcha();
					showAlertBox("绑定失败", "验证码错误！");
				}
			});

}

function changeCaptcha() {
	var random = Math.floor(Math.random() * 9999999);
	var url = loginHostUrl + "login/random1.jsp?otherOne=otherOne&d=" + random;
	$("#random").attr("src", url);
	$("#captcha-wrap").show();
}

/**
 * 刷新验证码
 */
function showOtherCaptcha() {
	var random = Math.floor(Math.random() * 9999999);
	var url = loginHostUrl + "login/random1.jsp?otherOne=otherOne&d=" + random;
	$("#random").attr("src", url);
}
